﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using umbraco.presentation;
using System.Xml;
using System.Xml.XPath;
using umbraco.presentation.nodeFactory;

namespace Umb.SimpleWebsite.Shared.Content
{
    public class Product
    {
        public Product()
        {

        }
        public XmlElement CurNode
        {
            get
            {
                return (XmlElement)UmbracoContext.Current.GetXml().SelectSingleNode("*");
            }
        }
        public XmlNodeList Nodes
        {
            get
            {
                XmlNodeList res = CurNode.SelectNodes("//*[name()='ProductDetail']");
                return res;
            }
        }

        public XPathNodeIterator GetCategories()
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<data/>");
            XmlElement root = (XmlElement)doc.SelectSingleNode("/*");
            List<string> excategories = new List<string>();
            foreach (XmlElement prod in Nodes)
            {
                //XmlElement groups = (XmlElement)prod.SelectSingleNode("groups");
                XmlElement categories = (XmlElement)prod.SelectSingleNode("category");
                if (categories != null)
                {
                    string[] ss = (categories.InnerText ?? "").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string s in ss)
                        if (!excategories.Exists(ec => ec.Equals(s))) excategories.Add(s);
                }
            }
            foreach (string item in excategories)
            {
                XmlElement c = doc.CreateElement("category");
                c.SetAttribute("id", item);
                int nodeid;
                if (int.TryParse(item, out nodeid))
                {
                    Node cnode = new Node(nodeid);
                    if (cnode != null && cnode.Id == nodeid)
                    {
                        Property p = cnode.GetProperty("title");
                        c.InnerText = p == null ? item : p.Value;
                    }
                }
                root.AppendChild(c);
            }
            return doc.CreateNavigator().Select(".");
        }
        public XPathNodeIterator GetCategoriesByProductId(string productId)
        {
            int productNodeId;
            List<string> categoriesId = null;
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<data/>");
            if (int.TryParse(productId, out productNodeId))
            {
                Node pnode = new Node(productNodeId);
                if (pnode != null && pnode.Id == productNodeId)
                {
                    Property categories = pnode.GetProperty("category");
                    if (categories != null)
                    {
                        categoriesId = (categories.Value ?? "").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
                    }
                    foreach (string item in categoriesId)
                    {
                        XmlElement c = doc.CreateElement("category");
                        c.SetAttribute("id", item);
                        int nodeid;
                        if (int.TryParse(item, out nodeid))
                        {
                            Node cnode = new Node(nodeid);
                            if (cnode != null && cnode.Id == nodeid)
                            {
                                Property p = cnode.GetProperty("title");
                                c.InnerText = p == null ? item : p.Value;
                            }
                        }
                        doc.DocumentElement.AppendChild(c);
                    }
                }
            }
            return doc.CreateNavigator().Select(".");
        }
    }
}
